<?php 

function add(){
// 1.参数完整性验证
  if (empty($_FILES['avatar'])) {//FILES!!!!!!!!!!!!!!!!!!!!
    $GLOBALS['error_message'] = '请上传头像';
    return;
  }

  if (empty($_POST['name'])) {
    $GLOBALS['error_message'] = '请输入姓名';
    return;
  }
  if (!isset($_POST['gender']) && $_POST['gender'] !== '-1') {
    $GLOBALS['error_message'] = '请选择性别';
    return;
  }

  if (empty($_POST['birthday'])) {
    $GLOBALS['error_message'] = '请选择出生日期';
    return;
  }

  // 取得数据
  $name = $_POST['name'];
  $gender = $_POST['gender'];
  $birthday = $_POST['birthday'];
  $avatar = $_FILES['avatar'];

  // 验证图片错误码
  if ($avatar['error'] !== UPLOAD_ERR_OK) {
    $GLOBALS['error_message'] = '图片上传失败';
    return;
  }

  // 移动图片
  // 不推荐文件名中含有中文
  $src = './assets/img/'.uniqid().mb_convert_encoding($avatar['name'],'GBK','UTF-8');//相对路径
  $back = move_uploaded_file($avatar['tmp_name'], $src);
  if (!back) {
    $GLOBALS['error_message'] = '图片上传失败';
    return;
  }


// 2.持久化
// 连接数据库
$connect = mysqli_connect('localhost','root','123456','users');
// var_dump($connect);
// 判断
if (!$connect) {
  exit('<h1>连接数据库失败</h1>');
}


// 执行添加操作
$query = mysqli_query($connect,"insert into info values (null, '{$src}', '{$name}', {$gender}, '{$birthday}');");
// var_dump($query);
// 判断
if (!$query) {
  exit('<h1>数据添加失败</h1>');
}

$affected_rows = mysqli_affected_rows($connect);
if ($affected_rows !== 1) {
  $GLOBALS['error_message'] = '添加数据失败';
  return;
}

// 关闭连接通道
mysqli_close($connect);

// 3.响应
header('Location:index.php');
}

// 表单是否提交
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  add();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>XXX管理系统</title>
  <link rel="stylesheet" href="assets/css/bootstrap.css">
  <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
  <nav class="navbar navbar-expand navbar-dark bg-dark fixed-top">
    <a class="navbar-brand" href="#">XXX管理系统</a>
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="index.html">用户管理</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">商品管理</a>
      </li>
    </ul>
  </nav>
  <main class="container">
    <h1 class="heading">添加用户</h1>
    <?php if (isset($error_message)): ?>
    <div class="alert alert-warning">
      <?php echo $error_message; ?>
    </div>
    <?php endif ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data" autocomplete="off">
      <div class="form-group">
        <label for="avatar">头像</label>
        <input type="file" class="form-control" name="avatar" id="avatar">
      </div>
      <div class="form-group">
        <label for="name">姓名</label>
        <input type="text" class="form-control" id="name" name="name">
      </div>
      <div class="form-group">
        <label for="gender">性别</label>
        <select class="form-control" id="gender" name="gender">
          <option value="-1">请选择性别</option>
          <option value="1">男</option>
          <option value="0">女</option>
        </select>
      </div>
      <div class="form-group">
        <label for="birthday">生日</label>
        <input type="date" class="form-control" id="birthday" name="birthday">
      </div>
      <button class="btn btn-primary">保存</button>
    </form>
  </main>
</body>
</html>
